//
//  UnarmedSearchVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/3.
//

import Foundation
import UIKit

let lattice = UserDefaults.standard.string(forKey: SafetyUserRole) == UserRole.Lattice.rawValue

class UnarmedSearchVC: SubLevelViewController {
    let detailV = UnarmedSearchView()

    open var didSelectItemWith:((_ param: AlarmWBFParam, _ dept: DeptModal) -> ())?

    var dept = DeptModal(deptId: BSUser.currentUser.deptId, deptName: BSUser.currentUser.deptName, isWd: lattice) {
        didSet {
            reloadData()
        }
    }
    
    var selDate: Date = Date() {
        didSet {
            reloadData()
        }
    }
    
    var type: SelectPopModal? {
        didSet {
            reloadData()
        }
    }
    
    var types: [SelectPopModal] = [SelectPopModal(id: "5", name: "撤防"), SelectPopModal(id: "6", name: "布防")]

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "布防记录查询"
        view.backgroundColor = .bg
        
        setupUI()
        
        reloadData()
        
        reloadTypes()
    }
    
    func reloadData() {
        detailV.netItem.value = dept.deptName
        detailV.dateItem.value = selDate.elTodayDate()
        if dept.isWd == true {
            detailV.typeItem.isHidden = false
            detailV.typeItem.snp.updateConstraints { make in
                make.height.equalTo(56)
            }
            detailV.typeItem.value = type?.name
        } else {
            detailV.typeItem.isHidden = true
            detailV.typeItem.snp.updateConstraints { make in
                make.height.equalTo(0)
            }
            detailV.typeItem.value = ""
        }
    }
    
    func reloadTypes() {
//        let param = AlarmCommonParam(dateType: "1", tbCompanyId: "\(deptId ?? -1)")
//        // 区域类型统计-撤布防
//        API.getAlarmCbfqyCount(withParam: param) { responseModel in
//            let models = responseModel.models ?? []
//            var popms: [SelectPopModal] = []
//            for m in models {
//                popms.append(SelectPopModal(id: m?.dictValue ?? "", name: m?.dictLabel ?? ""))
//            }
//            self.types = popms
//            self.type = popms.count > 0 ? popms[0] : nil
//        }
    }
        
    @objc func netSearchTapped() {
        let bSearchVC = BranchSearchVC()
        bSearchVC.didSelectBranchWith = { (dept, isWd) -> () in
            self.dept = dept
            self.dept.isWd = isWd
            self.detailV.netItem.value = dept.deptName
            bSearchVC.navigationController?.popViewController(animated: true)
        }
        navigationController?.pushViewController(bSearchVC, animated: true)
    }
    
    @objc func typeSearchTapped() {
        self.popup.bottomSheet(bgColor: .black.withAlphaComponent(0.65)) {
            let v = CornerView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 386))
            v.corners = SBRectCorner(topLeft: 12, topRight: 12, bottomLeft: 0, bottomRight: 0)
            v.backgroundColor = .white
            let rPopV = SelectPopView()
            rPopV.datas = types
            rPopV.didSelectItemWith = { (index, modal) -> () in
                self.popup.dismissPopup()
                self.type = modal
            }
            v.addSubview(rPopV)
            rPopV.snp.makeConstraints { (make) -> Void in
                make.top.right.left.bottom.equalToSuperview()
            }
            return v
        }
    }
    
    @objc func dateSearchTapped() {
        self.popup.bottomSheet(bgColor: .black.withAlphaComponent(0.65)) {
            let v = CornerView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 386))
            v.corners = SBRectCorner(topLeft: 12, topRight: 12, bottomLeft: 0, bottomRight: 0)
            v.backgroundColor = .white
            let rPopV = PickupPopView()
            rPopV.dateMode = .date
            rPopV.maximumDate = Date()
            rPopV.date = selDate
            rPopV.didSelectDateWith = { (picker, isComfirm) -> () in
                if isComfirm {
                    self.selDate = picker.date
                }
            }
            v.addSubview(rPopV)
            rPopV.snp.makeConstraints { (make) -> Void in
                make.top.right.left.bottom.equalToSuperview()
            }
            return v
        }
    }
    
    
    @objc func submitTapped() {
        if dept.isWd == true {
            navigationController?.pushViewController(UnarmedDetailVC(withDeptId: dept.deptId, deptName: dept.deptName, val: type?.id), animated: true)
        } else {
            didSelectItemWith?(AlarmWBFParam(date: selDate.elTodayDate(), deptId: "\(dept.deptId ?? -1)"), dept)
            navigationController?.popViewController(animated: true)
        }
    }
    
    // MARK: - Setup
    
    func setupUI() {
        detailV.netItem.addTarget(self, action: #selector(netSearchTapped), for: .touchUpInside)
        detailV.typeItem.addTarget(self, action: #selector(typeSearchTapped), for: .touchUpInside)
        detailV.dateItem.addTarget(self, action: #selector(dateSearchTapped), for: .touchUpInside)
        detailV.submitBtn.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)
        view.addSubview(detailV)
        detailV.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}
